﻿function Save(CaseType, CaseIndex) {
    DBNo = document.getElementById("divDBNo").value;
    CreatedDate = document.getElementById("txtCreatedDate").value;
    Payer = document.getElementById("divPayer").value;
    Subject = document.getElementById("divSubject").value;
    Attention = document.getElementById("divAttention").value;
    CurrencyIndex = document.getElementById("divCurrency").selectedIndex;
    Currency = document.getElementById("divCurrency").options[CurrencyIndex].value;
    caIndex = document.getElementById("divSelectChangeable").selectedIndex;

    Signer = document.getElementById("divSigner").value;
    Title = document.getElementById("divTitle").value;
    thueVat = document.getElementById("divPercentVat").value;

    ShortName = document.getElementById("txtShortCustomerName").value;
    CustomerName = document.getElementById("txtCustomerName").value;
    Address = document.getElementById("txtCustomerAddress").value;
    Phone = document.getElementById("txtCustomerPhone").value;
    pType = document.getElementById("drpSelectPType").options[document.getElementById("drpSelectPType").selectedIndex].value;

    CreditType = document.getElementById("drpSelectCreditType").options[document.getElementById("drpSelectCreditType").selectedIndex].value;


    fromLocationId = document.getElementById("selectFromDepartment").options[document.getElementById("selectFromDepartment").selectedIndex].value;
    toLocationId = document.getElementById("selectToDepartment").options[document.getElementById("selectToDepartment").selectedIndex].value;

    if (document.getElementById("divCaseType") != null) {
        CaseType = document.getElementById("divCaseType").innerHTML;
        CaseIndex = document.getElementById("divCaseIndex").value;
    }


    Max = 12;
    SaveItemData = "";
    for (var iIndex = 0; iIndex < Max; iIndex++) {
        Item = document.getElementById("txtIndex_" + iIndex).value;
        Description = document.getElementById("txtDescription_" + iIndex).value;
        UnitName = document.getElementById("txtUnit_" + iIndex).value;
        Quantity = document.getElementById("txtQuantity_" + iIndex).value;
        Price = document.getElementById("txtPrice_" + iIndex).value;

        SaveItemData = SaveItemData + Item;
        SaveItemData = SaveItemData + "~" + Description;
        SaveItemData = SaveItemData + "~" + UnitName;
        SaveItemData = SaveItemData + "~" + Quantity;
        SaveItemData = SaveItemData + "~" + Price;
        if (iIndex < Max - 1) {
            SaveItemData = SaveItemData + "|";
        }
    }

    EnglishAccount = document.getElementById("divAccountEnglish").value;
    VietnameseAccount = document.getElementById("divAccountVietnamese").value;
    DBNIndex = Portal.Vision.AddCreditNote.ServerSideAddCreditNote(CaseType, CaseIndex, DBNo, CreatedDate, Payer, Subject, Attention, Currency, Signer, Title, EnglishAccount, VietnameseAccount, thueVat, caIndex, ShortName, CustomerName, Address, Phone, pType, fromLocationId, toLocationId, CreditType, SaveItemData).value;
    if (DBNIndex != null) {
        if (DBNIndex.indexOf("ERROR") != -1) {
            alert(DBNIndex);
            return;
        }
    }

    Url = Portal.Vision.AddCreditNote.GetAddPaymentUrl(DBNIndex).value;
    if (confirm("Tạo thành công? Bạn có muốn thanh toán luôn?") == false) {
        Url = Portal.Vision.AddCreditNote.ServerSideGetViewUrl(DBNIndex).value;
    }
    window.open(Url, "_self");
}

function ClientDetectDebitNoFunc() {
    CaseIndex = document.getElementById("divCaseIndex").value;
    CaseType = document.getElementById("divCaseType").innerHTML;
    RetValue = Portal.Vision.AddCreditNote.GetDebitNodeNo(CaseType, CaseIndex).value;
    if (RetValue != null) {
        if (RetValue.indexOf("ERROR") != -1) {
            alert(RetValue);
            return;
        }
    }
    document.getElementById("divDBNo").value = RetValue;
}



function DetectAccountFunc() {
    selectedIndex = document.getElementById("divSelectAccount").selectedIndex;
    Id = document.getElementById("divSelectAccount").options[selectedIndex].value;
    Vietnamese = Portal.Vision.AddCreditNote.GetVietnamese(Id).value;
    English = Portal.Vision.AddCreditNote.GetEnglish(Id).value;
    document.getElementById("divAccountEnglish").value = English;
    document.getElementById("divAccountVietnamese").value = Vietnamese;
}

function DetectSignerTitle() {
    Signer = document.getElementById("divSigner").value;
    RetValue = Portal.Vision.AddCreditNote.ServerSideDetectSignerTitle(Signer).value;
    if (RetValue.indexOf("ERROR") != -1) {
        alert(RetValue);
        return;
    }
    document.getElementById("divTitle").value = RetValue;
}


function DetectOriginCustomer() {
    CustIndex = document.getElementById("divCustIndex").value;
    ShortName = Portal.Vision.AddCreditNote.ServerGetCustomerInfo(CustIndex, "ShortName").value;
    if (ShortName.indexOf("ERROR") != -1) {
        alert(ShortName);
        return;
    }

    document.getElementById("txtShortCustomerName").value = ShortName;

    CustName = Portal.Vision.AddCreditNote.ServerGetCustomerInfo(CustIndex, "CustName").value;
    if (CustName.indexOf("ERROR") != -1) {
        alert(CustName);
        return;
    }

    document.getElementById("txtCustomerName").value = CustName;

    Address = Portal.Vision.AddCreditNote.ServerGetCustomerInfo(CustIndex, "Address").value;
    if (Address.indexOf("ERROR") != -1) {
        alert(Address);
        return;
    }
    document.getElementById("txtCustomerAddress").value = Address;


    Phone = Portal.Vision.AddCreditNote.ServerGetCustomerInfo(CustIndex, "Phone").value;
    if (Phone.indexOf("ERROR") != -1) {
        alert(Address);
        return;
    }
    document.getElementById("txtCustomerPhone").value = Phone;
}